//
//  HCFindPayView.m
//  HappyChildren
//
//  Created by jiayibai-02por on 2017/6/12.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCFindPayView.h"
#import "AddAndLessView.h"

@interface HCFindPayView()

{
    UIImageView *payImageView;
    UILabel *payProductLabel;
    UILabel *payPriceLabel;
    CGRect *rootPayViewRect;
    UILabel *colorLabel;
    UILabel *sizeLabel;
    UILabel *countLabel;
    
    CGFloat originalY_;
    CGFloat originalX_;
    CGFloat originalY1;
    CGFloat originalX1;
    UIButton *currentButton;
    UIButton *currentButton1;
    
    UIButton *selectedColor;
    UIButton *selectedSize;
    
    
    NSInteger colorIndex_;  //选择的颜色
    NSInteger sizeIndex_;   //选择的尺码
    NSInteger goodsSum_;    //选择的数量
}
@property (nonatomic, strong) AddAndLessView *addView;

@end

@implementation HCFindPayView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        colorIndex_ = 1000;
        sizeIndex_ = 1000;
        goodsSum_ = 1;
        [self initializeView];
    }
    return self;
}

- (void)initializeView{
    
    payImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, -10, 60, 60)];
    payImageView.backgroundColor = [UIColor whiteColor];
    payImageView.layer.cornerRadius = 4.0;
    payImageView.layer.masksToBounds = YES;
    payImageView.layer.borderColor = YBKRGBCOLOR(240, 240, 240, 1).CGColor;
    payImageView.layer.borderWidth = 0.5;
    [self addSubview:payImageView];
    
    payProductLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, self.frame.size.width-100, 25)];
    payProductLabel.text = @"韩国气质款兔毛羊羔戎外套";
    [self addSubview:payProductLabel];
    
    payPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 35, self.frame.size.width-100, 15)];
    payPriceLabel.text = @"￥356.0";
    payPriceLabel.textColor = YBKRGBCOLOR(240, 96, 96, 1);
    [self addSubview:payPriceLabel];
    
    colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, payPriceLabel.frame.origin.y+payPriceLabel.frame.size.height+20, 50, 15)];
    colorLabel.textColor = [UIColor darkGrayColor];
    colorLabel.font = [UIFont systemFontOfSize:16];
    colorLabel.text = @"颜色:";
    [self addSubview:colorLabel];
    
    sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, colorLabel.frame.origin.y+colorLabel.frame.size.height+50, 50, 15)];
    sizeLabel.font = [UIFont systemFontOfSize:16];
    sizeLabel.textColor = [UIColor darkGrayColor];
    sizeLabel.text = @"尺码:";
    [self addSubview:sizeLabel];
    
    countLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, sizeLabel.frame.origin.y+sizeLabel.frame.size.height+50, 50, 15)];
    countLabel.font = [UIFont systemFontOfSize:16];
    countLabel.textColor = [UIColor darkGrayColor];
    countLabel.text = @"数量:";
    [self addSubview:countLabel];
    
    UIButton *sureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height-50, self.frame.size.width, 50)];
//    sureButton.layer.cornerRadius = 3.0;
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton setTitle:@"选好了" forState:UIControlStateNormal];
    [sureButton setBackgroundColor:YBKRGBCOLOR(240, 96, 96, 1)];
    [sureButton addTarget:self action:@selector(sureButtonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sureButton];
    
//    //划线
//    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, sureButton.frame.origin.y, Screen_width, 1)];
//    lineView.backgroundColor = [UIColor lightGrayColor];
//    [self addSubview:lineView];
    
    self.addView = [[AddAndLessView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(countLabel.frame), countLabel.center.y-25, 200, 50)];
    self.addView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.addView];
    
    __weak typeof (self)weakSelf = self;
    weakSelf.addView.clickedString = ^(NSString *aa) {
        
        goodsSum_ = [aa integerValue];
    };
    goodsSum_ = 1;
}

#pragma mark -- 选好了
- (void)sureButtonAction1:(UIButton *)sender {

    //选好了
    if (colorIndex_ == 1000 && self.typeArray.count > 0) {
        
        [XHToast showBottomWithText:@"请选择颜色"];
        return;
    }
    if (sizeIndex_ == 1000 && self.typeArray.count > 0) {
        
        [XHToast showBottomWithText:@"请选择尺码"];
        return;
    }
    NSString *color = self.colorArray[colorIndex_];
    NSString *size = self.sizeArray[sizeIndex_];
    NSString *sum = [NSString stringWithFormat:@"%ld",goodsSum_];
    
    if (self.sureButtonClick) {
        
        self.sureButtonClick(color, size, sum);
    }
}

- (void)setColorArray:(NSArray *)colorArray {

    _colorArray = colorArray;
    
    originalY_ = colorLabel.center.y - 12.5;
    originalX_ = CGRectGetMaxX(colorLabel.frame);
    if (colorArray.count > 0) {
        
        for (UIButton *button in self.subviews) {
            
            if ([button isKindOfClass:[UIButton class]] && button.tag >= 10) {
                
                [button removeFromSuperview];
            }
        }
    }
    
    for (int i = 0; i < colorArray.count; i++) {
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(colorLabel.frame), colorLabel.center.y-15, 0, 0)];
        button.tag = 10 + i;
        button.layer.cornerRadius = 3.0;
        button.layer.borderColor = [UIColor grayColor].CGColor;
        button.layer.borderWidth = 0.5;
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitle:colorArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(colorButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
        NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGSize size = [colorArray[i] sizeWithAttributes:attribute];
        if (currentButton == nil) {
            
            [button setFrame:CGRectMake(originalX_, colorLabel.center.y-12.5, size.width + 30, 25)];
        }
        else {
            
            if (CGRectGetMaxX(currentButton.frame)+ size.width+30 - (Screen_width-15-50-10) > 0) {
                
                originalY_ += 33;
                originalX_ = CGRectGetMaxX(colorLabel.frame);
            }
            [button setFrame:CGRectMake(originalX_, originalY_, size.width+30, 25)];
        }
        
        currentButton = button;
        originalX_ += currentButton.frame.size.width+ 5;
    }
}

- (void)setSizeArray:(NSArray *)sizeArray {

    _sizeArray = sizeArray;
    
    originalY1 = sizeLabel.center.y - 12.5;
    originalX1 = CGRectGetMaxX(sizeLabel.frame);
    if (sizeArray.count > 0) {
        
        for (UIButton *button in self.subviews) {
            
            if ([button isKindOfClass:[UIButton class]] && button.tag >= 50) {
                
                [button removeFromSuperview];
            }
        }
    }
    
    for (int i = 0; i < sizeArray.count; i++) {
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(sizeLabel.frame), sizeLabel.center.y-15, 0, 0)];
        button.tag = 50 + i;
        button.layer.cornerRadius = 3.0;
        button.layer.borderColor = [UIColor grayColor].CGColor;
        button.layer.borderWidth = 0.5;
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitle:sizeArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(sizeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGSize size = [sizeArray[i] sizeWithAttributes:attribute];
        if (currentButton1 == nil) {
            
            [button setFrame:CGRectMake(originalX1, sizeLabel.center.y-12.5, size.width + 30, 25)];
        }
        else {
            
            if (CGRectGetMaxX(currentButton1.frame)+ size.width+30 - (Screen_width-15-50-10) > 0) {
                
                originalY1 += 33;
                originalX1 = CGRectGetMaxX(sizeLabel.frame);
            }
            [button setFrame:CGRectMake(originalX1, originalY1, size.width+30, 25)];
        }
        
        currentButton1 = button;
        originalX1 += currentButton1.frame.size.width+ 5;
    }
}

- (void)colorButtonClick:(UIButton *)sender {

    if (sender == selectedColor) {
        
        sender.backgroundColor = [UIColor whiteColor];
        [sender setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        selectedColor = nil;
        [self calculateMethodWithIndex:1000 type:1];
        [selectedSize setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        //取消选择
        colorIndex_ = 1000;
    }
    else {
    
        sender.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        selectedColor.backgroundColor = [UIColor whiteColor];
        [selectedColor setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        selectedColor = sender;
        [self calculateMethodWithIndex:sender.tag-10 type:1];
        
        //选择
        colorIndex_ = sender.tag-10;
    }
}

- (void)sizeButtonClick:(UIButton *)sender {

    if (sender == selectedSize) {
        
        sender.backgroundColor = [UIColor whiteColor];
        [sender setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        selectedSize = nil;
        [self calculateMethodWithIndex:1000 type:2];
        [selectedColor setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        //取消选择
        sizeIndex_ = 1000;
    }
    else {
    
        sender.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        selectedSize.backgroundColor = [UIColor whiteColor];
        [selectedSize setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        selectedSize = sender;
        [self calculateMethodWithIndex:sender.tag-50 type:2];
        
        //选择
        sizeIndex_ = sender.tag-50;
    }
}

- (void)setTypeArray:(NSArray *)typeArray {

    _typeArray = typeArray;
    NSMutableArray *colors = [NSMutableArray array];
    NSMutableArray *sizes = [NSMutableArray array];
    for (int i = 0; i < typeArray.count; i++) {
        
        NSString *color = [[typeArray objectAtIndex:i] objectForKey:@"color"];
        NSString *size = [NSString stringWithFormat:@"%@",[[typeArray objectAtIndex:i] objectForKey:@"size"]];
        
        if ([colors containsObject:color]) {
            return;
        }else{
            [colors addObject:color];

        }
        
        if ([sizes containsObject:size]) {
            return;
        }else{
            [sizes addObject:size];
        }
    }
    if (typeArray.count > 0) {
        
        self.colorArray = colors;
        self.sizeArray = sizes;
    }
}

- (void)calculateMethodWithIndex:(NSInteger)index type:(NSInteger)type {

    if (type==1) {
        
        if (index == 1000) {
            
            for (int i = 0; i < self.sizeArray.count; i++) {
                
                UIButton *button = [self viewWithTag:50 + i];
                button.userInteractionEnabled = YES;
                if (selectedSize == button) {
                    
                    [selectedSize setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                }
                else {
                
                    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                    button.layer.borderColor = [UIColor grayColor].CGColor;
                }
            }
        }
        else {
        
            //点击选择颜色
            NSString *color = self.colorArray[index];       //点击的颜色
            for (int i = 0; i < self.typeArray.count; i++) {    //编列颜色，尺码数组
                
                if ([[self.typeArray[i] objectForKey:@"color"] isEqualToString:color]) {
                    
                    NSString *size = [NSString stringWithFormat:@"%@",[self.typeArray[i] objectForKey:@"size"]];
                    for (int i = 0; i < self.sizeArray.count; i++) {
                        
                        UIButton *button = [self viewWithTag:50+i];
                        if ([size isEqualToString:self.sizeArray[i]]) {
                            
                            button.layer.borderColor = [UIColor grayColor].CGColor;
                            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                            button.userInteractionEnabled = YES;
                            if (button == selectedSize) {
                                
                                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                            }
                        }
                        else {
                            
                            button.layer.borderColor = [UIColor lightGrayColor].CGColor;
                            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                            button.userInteractionEnabled = NO;
                        }
                        
                    }
                }
            }
        }
    }
    else {
    
        //点击选择尺码
        
        if (index == 1000) {
            
            for (int i = 0; i < self.colorArray.count; i++) {
                
                UIButton *button = [self viewWithTag:10 + i];
                button.userInteractionEnabled = YES;
                NSLog(@"dasdiuasiud");
                if (selectedColor == button) {
                    
                    [selectedColor setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                }
                else {
                    
                    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                    button.layer.borderColor = [UIColor grayColor].CGColor;
                }
            }
        }
        else {
        
            NSString *size = self.sizeArray[index];
            for (int i = 0; i < self.typeArray.count; i++) {
                
                if ([[self.typeArray[i] objectForKey:@"size"] isEqualToString:size]) {
                    
                    NSString *color = [NSString stringWithFormat:@"%@",[self.typeArray[i] objectForKey:@"color"]];
                    for (int i = 0; i < self.colorArray.count; i++) {
                        
                        UIButton *button = [self viewWithTag:10+i];
                        if ([color isEqualToString:self.colorArray[i]]) {
                            
                            button.layer.borderColor = [UIColor grayColor].CGColor;
                            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                            button.userInteractionEnabled = YES;
                            
                            if (button == selectedColor) {
                                
                                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                            }
                        }
                        else {
                            
                            button.layer.borderColor = [UIColor lightGrayColor].CGColor;
                            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                            button.userInteractionEnabled = NO;
                        }
                    }
                }
            }
        }
    }
}

- (void)setGoodsImage:(NSString *)goodsImage {

    _goodsImage = goodsImage;
    [payImageView sd_setImageWithURL:[NSURL URLWithString:goodsImage] placeholderImage:nil];
}


-(void)setTitlename:(NSString *)titlename {
    payProductLabel.text = [NSString stringWithFormat:@"%@",titlename];
}

-(void)setGoodprice:(NSString *)goodprice {
    payPriceLabel.text = [NSString stringWithFormat:@"%@",goodprice];
}

@end
